package com.n9.qq.android.login.tlv;

import com.n9.qq.util.Crypter;
import com.n9.qq.util.Util;
import org.apache.commons.codec.digest.DigestUtils;

/**
 * @author ice
 * @since 2019/2/11 12:15
 */
public class Tlv0106 extends BaseTlv {
    int len = 98;
    int h = 4;
    int i = 5;

    public Tlv0106() {
        this.cmd = 0x0106;
    }

    /**
     * @param qqUin
     * @param pwMd5
     * @param tgtgtKey
     * @param imeiMd5
     * @param serverTime
     * @param appid
     * @return
     */
    public byte[] getData(long qqUin, byte[] pwMd5, byte[] tgtgtKey, byte[] imeiMd5, long serverTime, long appid) {
        byte[] qqUinStrBytes = Long.toString(qqUin).getBytes();
        this.len += qqUinStrBytes.length + 2;

        byte[] var14 = new byte[this.len];
        Util.int16_to_buf(var14, 0, this.h);
        Util.int32_to_buf(var14, 2, Util.get_rand_32());
        Util.int32_to_buf(var14, 6, this.i);
        Util.int32_to_buf(var14, 10, (int)0x10L);
        Util.int32_to_buf(var14, 14, 0);
        Util.int64_to_buf(var14, 18, qqUin);
        byte[] serverTimeData = Util.int64ToBuf32(serverTime);
        System.arraycopy(serverTimeData, 0, var14, 26, serverTimeData.length);
        int index = serverTimeData.length + 26;

        byte[] var20 = new byte[4];
        System.arraycopy(var20, 0, var14, index, var20.length);
        index += var20.length;
        Util.int8_to_buf(var14, index, 1);
        ++index;
        System.arraycopy(pwMd5, 0, var14, index, pwMd5.length);
        index += pwMd5.length;
        System.arraycopy(tgtgtKey, 0, var14, index, tgtgtKey.length);
        index += tgtgtKey.length;
        Util.int32_to_buf(var14, index, 0);
        index += 4;
        Util.int8_to_buf(var14, index, 1);
        ++index;
        System.arraycopy(imeiMd5, 0, var14, index, imeiMd5.length);
        index += imeiMd5.length;

        Util.int64_to_buf32(var14, index, appid);
        index += 4;
        Util.int32_to_buf(var14, index, 0x01);
        index += 4;
        Util.int16_to_buf(var14, index, qqUinStrBytes.length);
        System.arraycopy(qqUinStrBytes, 0, var14, index + 2, qqUinStrBytes.length);

        byte[] keyData = new byte[24];
        System.arraycopy(pwMd5, 0, keyData, 0, pwMd5.length);
        Util.int64_to_buf(keyData, 16, qqUin);

        byte[] key = DigestUtils.md5(keyData);
        byte[] enData = new Crypter().encrypt(var14, key);
        return this.getBufData(enData);
    }


}
